import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AddJiShiPage extends StatefulWidget {
  bool isEdit;    //true:修改 false:添加
  Map jishi=new Map();   //技师信息
  AddJiShiPage({Key key, this.title,this.isEdit,this.jishi}) : super(key: key);

  final String title;

  @override
  _AddJiShiPage createState() => _AddJiShiPage(isEdit,jishi);
}

class _AddJiShiPage extends State<AddJiShiPage> with SingleTickerProviderStateMixin {
  TextEditingController _typeNameController; // 技师名字文本框控制器
  TextEditingController _jishiPhoneController; // 技师电话文本框控制器
  TextEditingController _jishiJieshaoController; // 技师介绍文本框控制器
  String merchantID="";  //商户ID
  List<DropdownMenuItem> mTypeNameList =[];//商户类型名称集合
  bool isEdit = true;    //true是修改,false是新增
  SharedPreferences prefs;
  dynamic _imgPath;
  var tu;
  Map jishi=new Map();
  String id=''; //技师的id

  // 构造方法
  _AddJiShiPage(isEdit,jishi){
    this.isEdit=isEdit;
    this.jishi=jishi;
    _typeNameController = new TextEditingController();
    _jishiPhoneController = new TextEditingController();
    _jishiJieshaoController = new TextEditingController();
    if(isEdit){
      getJishiMsg(jishi);
    }
  }

  //获取技师的信息
  void getJishiMsg(value) async {
      _typeNameController.text=value['name'];
      _jishiPhoneController.text=value['phone'];
      _jishiJieshaoController.text=value['jieshao'];
      tu=value['picture'];
      id=value['id'];
      if(mounted)
      setState(() {

      });
  }

  /*图片控件*/
  Widget _ImageView(imgPath,tu) {
    if (tu == null) {
      return Center(
        child: Text("请选择图片或拍照"),
      );
    } else {
      if(tu.toString().startsWith('upload')){
        return Image.network(Config.getInstance().getHost()+tu,width: ScreenUtil().setWidth(200),height: ScreenUtil().setHeight(200),);
      }else{
        return Image.network(Config.getInstance().getHost()+"tmp/"+tu,width: ScreenUtil().setWidth(200),height: ScreenUtil().setHeight(200),);
      }

    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        title: Text("新增技师"),
        actions: [
          IconButton(icon: Icon(Icons.save), onPressed: () async {
            SharedPreferences prefs = await SharedPreferences.getInstance();
            merchantID = prefs.getString("merchantID");
            var map = new Map<String,dynamic>();
            map['name']=_typeNameController.text.toString();
            map['phone']=_jishiPhoneController.text.toString();
            map['jieshao']=_jishiJieshaoController.text.toString();
            map['picture']=tu.toString().startsWith('upload')?tu:"upload/jishi/"+tu;
            map['mId']=merchantID.toString();//商户ID
            var res;
            if(isEdit){
              print(id);
              res=await HttpUtil().get("/waimai/merchantJishi/updateJishi?name="+map['name']+"&phone="+map['phone']+"&jieshao="+map['jieshao']+"&picture="+tu+"&id="+id);
            }else{
              res=await HttpUtil().get("/waimai/merchantJishi/addMerchantJishi?name="+map['name']+"&phone="+map['phone']+"&jieshao="+map['jieshao']+"&picture="+tu+"&mId="+map['mId']);
            }
            if(res['code']==200){
              if(isEdit){
                Fluttertoast.showToast(msg: "编辑成功");
                map['id']=id;
              }else{
                Fluttertoast.showToast(msg: "添加成功");
                map['id']=res['data'];
              }
              Future.delayed(Duration(seconds: 1), () {
                MyRoute.pop(map);
              });
            }else{
              Fluttertoast.showToast(
                  msg: res['msg']
              );
            }
          }
          )
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("技师名称"),SizedBox(width: 5,),],),
                  SizedBox(width: 30,),
                  Expanded(
                      child: TextFormField(
                      controller: _typeNameController,
                      decoration: InputDecoration(
                        hintText: "输入技师名称",
                        hintStyle: TextStyle(fontSize: 14)
                      ),
                    )
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("技师电话"),SizedBox(width: 5,),],),
                  SizedBox(width: 30,),
                  Expanded(
                      child: TextFormField(
                        controller: _jishiPhoneController,
                        decoration: InputDecoration(
                            hintText: "输入技师电话号码",
                            hintStyle: TextStyle(fontSize: 14)
                        ),
                      )
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("技师介绍"),SizedBox(width: 5,),],),
                  SizedBox(width: 30,),
                  Expanded(
                      child: Container(
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.black54 , width: 1) ,
                          // color: Colors.white,
                        ),
                        child: TextFormField(
                          controller: _jishiJieshaoController,
                          maxLines: 5,
                          decoration: InputDecoration(
                              hintText: "输入技师介绍",
                              hintStyle: TextStyle(fontSize: 14)
                          ),
                        ),
                      )
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("技师照片"),SizedBox(width: 5,),],),
                  SizedBox(width: 30,),
                  GestureDetector(child: Row(
                    children: [
                      tu==null?Text(""): _ImageView(_imgPath,tu),
                      tu!=null?Text(""): Container(
                        width: ScreenUtil().setWidth(200),
                        height: ScreenUtil().setHeight(200),
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.black , width: 0) ,
                        ),
                        child: Center(child: Text("+",style: TextStyle(fontSize: 30),),),
                      ),
                    ],
                  ),onTap: () async {
                    var s = await ImagePicker.pickImage(source: ImageSource.gallery);
                    _imgPath=s;
                    File file = new File(s.path);// 获取文件路径
                    var res = await HttpUtil().upload("/upload", file);
                    tu = res['data'];
                    setState(()  {

                    });
                  },),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}